package com._520it.wms.service.impl;

import com._520it.wms.domain.*;
import com._520it.wms.mapper.ProductStockMapper;
import com._520it.wms.mapper.StockIncomeBillItemMapper;
import com._520it.wms.mapper.StockIncomeBillMapper;
import com._520it.wms.query.PageResult;
import com._520it.wms.query.QueryObject;
import com._520it.wms.service.IProductService;
import com._520it.wms.service.IProductStockService;
import com._520it.wms.service.IStockIncomeBillService;
import com._520it.wms.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by HASEE on 2017/11/14.
 */
@Service
public class StockIncomeBillServiceImpl implements IStockIncomeBillService {
    @Autowired
    private StockIncomeBillMapper stockIncomeBillMapper;
    @Autowired
    //注入库存
    private StockIncomeBillItemMapper stockIncomeBillItemMapper;
    @Autowired
    private IProductStockService productStockService;

    @Override
    public void saveOrUpdate(StockIncomeBill stockIncomeBill) {
        if (stockIncomeBill.getId() != null) {
            //编辑操作
            //根据入库单删除原来的订单明细
            stockIncomeBillItemMapper.deleteItemBillId(stockIncomeBill.getId());
            //设置总金额/中数量
            BigDecimal totalNumber = BigDecimal.ZERO;
            BigDecimal totalAmount = BigDecimal.ZERO;
            //遍历出价格和数量
            for (StockIncomeBillItem item : stockIncomeBill.getItems()) {
                BigDecimal number = item.getNumber();
                BigDecimal costPrice = item.getCostPrice();
                BigDecimal amount = number.multiply(costPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
                item.setAmount(amount);
                totalAmount = totalAmount.add(amount);
                totalNumber = totalNumber.add(number);
                //保存入库明细
                //设置关联关系
                item.setBillId(stockIncomeBill.getId());
                stockIncomeBillItemMapper.insert(item);
            }
            stockIncomeBill.setTotalAmount(totalAmount);
            stockIncomeBill.setTotalNumber(totalNumber);
            //更新入库单
            stockIncomeBillMapper.updateByPrimaryKey(stockIncomeBill);
        } else {//新增
            //1设置制单人
            stockIncomeBill.setInputUser(UserContext.getCurrentUser());
            //2设置制单时间
            stockIncomeBill.setInputTime(new Date());
            //3设置制单状态为未审核 NORMAL=0
            stockIncomeBill.setStatus(StockIncomeBill.NORMAL);
            //4计算总金额,计算总数量
            BigDecimal totalAmount = BigDecimal.ZERO;
            BigDecimal totalNumber = BigDecimal.ZERO;
            //遍历出价格和数量在明细表中
            for (StockIncomeBillItem item : stockIncomeBill.getItems()) {
                BigDecimal number = item.getNumber();//数量
                BigDecimal costPrice = item.getCostPrice();//价格
                BigDecimal amount = number.multiply(costPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
                item.setAmount(amount);
                totalNumber = totalNumber.add(number);
                totalAmount = totalAmount.add(amount);
            }
            stockIncomeBill.setTotalAmount(totalAmount);
            stockIncomeBill.setTotalNumber(totalNumber);
            //5保存入库单
            stockIncomeBillMapper.insert(stockIncomeBill);
            //6保存入库单明细
            for (StockIncomeBillItem item : stockIncomeBill.getItems()) {
                //建立关联表关系
                item.setBillId(stockIncomeBill.getId());
                stockIncomeBillItemMapper.insert(item);
            }


        }

    }

    @Override
    public void delete(Long id) { //查询主键id
        StockIncomeBill stockIncomeBill = stockIncomeBillMapper.selectByPrimaryKey(id);
        //判断是否审核
        if (StockIncomeBill.NORMAL == stockIncomeBill.getStatus()) {
            //删除入库明细
            stockIncomeBillItemMapper.deleteItemBillId(id);
            //删除主键
            stockIncomeBillMapper.deleteByPrimaryKey(id);
        }

    }

    @Override
    public StockIncomeBill get(Long id) {
        return stockIncomeBillMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StockIncomeBill> listAll() {
        return stockIncomeBillMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int totalCount = stockIncomeBillMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getCurrentPage(), qo.getPageSize());
        }
        //查询结果集
        List<StockIncomeBill> list = stockIncomeBillMapper.queryForList(qo);
        return new PageResult(qo.getCurrentPage(), qo.getPageSize(), list, totalCount);
    }

    @Override
    //库存信息
    public void audit(Long id) {
        //采购入库订单审核
        //判断单据的状态 是未审核的单据操作审核
        StockIncomeBill stockIncomeBill = stockIncomeBillMapper.selectByPrimaryKey(id);
        if (StockIncomeBill.NORMAL == stockIncomeBill.getStatus()) {
            //设置审核时间
            stockIncomeBill.setAuditTime(new Date());
            //设置审核状态
            stockIncomeBill.setStatus(StockIncomeBill.AUDIT);
            //设置审核人
            stockIncomeBill.setAuditor(UserContext.getCurrentUser());
            //调用审核操作
            stockIncomeBillMapper.audit(stockIncomeBill);
            //修改库存信息
            //遍历出商品入库明细
            Depot depot = stockIncomeBill.getDepot();
            for (StockIncomeBillItem item : stockIncomeBill.getItems()) {
                        productStockService.income(item.getProduct(),depot,item.getNumber(),item.getAmount());
                }
        } else {
            throw new RuntimeException("已经审核的单据不能重复审核");

        }
    }
}
